package com.yhc.mq.mq.consumer;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@Configuration
public class MyRocketMessageListener implements MessageListener {


    @Override
    public Action consume(Message message, ConsumeContext context) {

        // 业务处理逻辑
        log.info(">>>>> 【消费者】系统时间:[{}]接收消息MsgId=[{}]，消息内容:{}", LocalDateTime.now(), message.getMsgID(), new String(message.getBody(), StandardCharsets.UTF_8));
        try {
            // todo 业务逻辑处理
            return Action.CommitMessage;
        } catch (Exception e) {
            //消费失败
            log.error(">>>>> 【消费者】系统时间:[{}]处理消息MsgId=[{}], 当前消费失败, 开始重试......！", LocalDateTime.now(), message.getMsgID());
            return Action.ReconsumeLater;
        }
    }
}

